<!--
 * @Author: your name
 * @Date: 2021-06-17 18:59:10
 * @LastEditTime: 2021-06-18 18:21:32
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \agriculture-ui\src\pages\cms\Article.vue
-->
<template>
  <div>
    <!-- 搜索 -->
    <el-form inline :model="param" size="small">
      <el-form-item label="标题">
        <el-input v-model="param.title"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData">搜索</el-button>
        <el-button @click="toPublishHandler">发布资讯</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="articles.list"
      style="width: 100%">
      <el-table-column prop="title" label="标题"> </el-table-column>
      <el-table-column prop="status" label="状态" width="180"> </el-table-column>
      <el-table-column prop="readTimes" label="阅读次数" width="200"> </el-table-column>
      <el-table-column label="操作" width="200" align="center"> 
        <!-- 定制 -->
        <template v-slot="scope">
          <el-button @click="delHandler(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      @current-change="pageChangeHandler"
      :current-page="articles.page"
      :page-size="articles.pageSize"
      :total="articles.total">
    </el-pagination>
    <!-- 模态框 -->
    <el-dialog title="发布资讯" :visible.sync="dialogVisible"
      width="60%">
  
      <el-form size="small" :model="form">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input type="textarea" :rows="10" v-model="form.content"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="publishHandler">确 定</el-button>
      </span>
    </el-dialog>
    <!-- /模态框 -->
  </div>
</template>
<script>
import {get,post} from '@/utils/request'
export default {
  // 1. 数据，变量
  data(){
    return {
      dialogVisible:false,
      title:"文章管理",
      articles:[],
      param:{
        page:1,
        pageSize:5
      },
      form:{}
    }
  },
  // 2. 方法
  methods:{
    //2.5 发布资讯
    publishHandler(){
      let url = "/article/saveOrUpdate"
      post(url,this.form).then(resp => {
        // 提示
        this.$message({type:"success",message:resp.message})
        // 关闭模态框
        this.dialogVisible = false;
        // 刷新
        this.loadData();
      })
    },
    //2.4 去发布资讯
    toPublishHandler(){
      this.dialogVisible = true;
    },
    //2.3 翻页
    pageChangeHandler(page){
      this.param.page = page;
      this.loadData();
    },
    //2.2 根据id删除
    delHandler(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/article/deleteById"
        get(url,{id:row.id}).then( resp => {
          // 提示删除成功
          this.$message({type:"success",message:resp.message})
          // 刷新页面数据
          this.loadData();
        });
      })
    },
    //2.1 加载数据
    loadData(){
      let url = "/article/pageQuery";
      get(url,this.param).then( resp => {
        this.articles = resp.data;
      });
    }
  },
  // 3. 生命周期，在页面加载出来之前
  created(){
    this.loadData();
  }
}
</script>